package dao.impl;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import dao.DishAuditDao;
import po.DishAudit;

@Repository("dishAuditDao")
public class DishAuditDaoImpl implements DishAuditDao{
	
	@PersistenceContext(name="un")
	private EntityManager entityManager;

	@Transactional
	@Override
	public String insertDishAudit(DishAudit dishAudit) {
		// TODO Auto-generated method stub
		
		entityManager.persist(dishAudit);
		
		return dishAudit.getdId();
	}

	@Override
	public List<DishAudit> listAllDishAudit() {
		// TODO Auto-generated method stub
		String jpql = "from DishAudit da order by da.status, da.createDate asc";
		List<DishAudit> audits = entityManager.createQuery(jpql).getResultList();
		return audits;
	}

	@Override
	public DishAudit findDishAuditById(String id) {
		// TODO Auto-generated method stub
		DishAudit dishAudit = entityManager.find(DishAudit.class, id);
		return dishAudit;
	}

	@Transactional
	@Override
	public String updateDishAudit(DishAudit dishAudit) {
		// TODO Auto-generated method stub
		
		entityManager.merge(dishAudit);
		
		return dishAudit.getdId();
	}

	@Override
	public List<String> findRecommendDish() {
		// TODO Auto-generated method stub
		String jpql = "select da.dId from DishAudit da where da.expiredDate > :today and da.status = 1 order by da.createDate desc";
		List<String> list = entityManager.createQuery(jpql)
				.setParameter("today", new Date())
				.setMaxResults(5).getResultList();
		return list;
	}

	@Override
	public int removeDishAudit(DishAudit dishAudit) {
		// TODO Auto-generated method stub
		entityManager.remove(dishAudit);
		return 1;
	}

	@Override
	public List<DishAudit> listDishAuditByMId(String mId) {
		// TODO Auto-generated method stub
		String jpql = "from DishAudit da where da.mId = :mId";
		List<DishAudit> list = entityManager.createQuery(jpql)
				.setParameter("mId", mId).getResultList();
		return list;
	}
	
	
	
}
